package com.base2.webshop.server.locators;

import org.springframework.orm.jpa.JpaTemplate;

import com.base2.webshop.server.entities.EntityBase;
import com.base2.webshop.util.ApplicationContextProvider;
import com.google.gwt.requestfactory.shared.Locator;

public class AbstractEntityLocator extends Locator<EntityBase, Long>{

	private JpaTemplate jpaTemplate;
	
	public AbstractEntityLocator(){
		jpaTemplate=(JpaTemplate)ApplicationContextProvider.getApplicationContext().getBean("jpaTemplate");
	}
	@Override
	public EntityBase create(Class<? extends EntityBase> clazz){
		try {
			return clazz.newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public EntityBase find(Class<? extends EntityBase> clazz, Long id) {
		return jpaTemplate.find(clazz, id);
	}

	@Override
	public Class<EntityBase> getDomainType() {
		return EntityBase.class;
	}

	@Override
	public Long getId(EntityBase entity) {
		return entity.getId();
	}

	@Override
	public Class<Long> getIdType() {
		return Long.class;
	}

	@Override
	public Object getVersion(EntityBase entity) {
		return entity.getVersion();
	}

}
